package com.atguigu.lease.web.admin.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;



    //自定义房间信息添加或修改
    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //根据有无房间id进行删除
        Long id = roomSubmitVo.getId();
        if (id != null){//代表有id值，进行删除
            //1.删除房间相关图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            queryWrapper.eq(GraphInfo::getItemId,id);
            graphInfoService.remove(queryWrapper);
            //2.删除房间相关属性信息
            LambdaQueryWrapper<RoomAttrValue> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(RoomAttrValue::getRoomId,id);
            roomAttrValueService.remove(queryWrapper1);
            //3.删除房间相关配套信息
            LambdaQueryWrapper<RoomFacility> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(RoomFacility::getRoomId,id);
            roomFacilityService.remove(queryWrapper2);
            //4.删除房间相关标签信息
            LambdaQueryWrapper<RoomLabel> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(RoomLabel::getRoomId,id);
            roomLabelService.remove(queryWrapper3);
            //5.删除房间相关支付方式
            LambdaQueryWrapper<RoomPaymentType> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(RoomPaymentType::getRoomId,id);
            roomPaymentTypeService.remove(queryWrapper4);
            //6.删除房间相关可选租期
            LambdaQueryWrapper<RoomLeaseTerm> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(RoomLeaseTerm::getRoomId,id);
            roomLeaseTermService.remove(queryWrapper5);
        }
        //添加基础信息,id自动回显
        this.saveOrUpdate(roomSubmitVo);
        //1.添加图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoService.saveOrUpdate(graphInfo);
            }
        }
        //2.添加属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue build = RoomAttrValue.builder().roomId(roomSubmitVo.getId())
                        .attrValueId(attrValueId).build();
                roomAttrValueService.saveOrUpdate(build);
            }
        }
        //3.添加配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility.builder().roomId(roomSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                roomFacilityService.saveOrUpdate(build);
            }
        }
        //4.添加标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel.builder().roomId(roomSubmitVo.getId())
                        .labelId(labelInfoId).build();
                roomLabelService.saveOrUpdate(build);
            }
        }
        //5.添加支付信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId())
                        .paymentTypeId(paymentTypeId).build();
                roomPaymentTypeService.saveOrUpdate(build);
            }
        }
        //6.添加租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId())
                        .leaseTermId(leaseTermId).build();
                roomLeaseTermService.saveOrUpdate(build);
            }
        }
    }

    //根据请求条件分页查询房间信息
    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page,queryVo);

    }

    //根据id查询房间信息
    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1.查询对应房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //2.查询对应公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        //3.查询对应图片列表信息
        List<GraphVo> graphVoList = graphInfoService.selectListByItemTypeAndId(ItemType.ROOM,roomInfo.getId());
        //4.查询对应属性列表信息
        List<AttrValueVo> attrValueVoList = roomAttrValueService.selectListByRoomInfoId(roomInfo.getId());
        //5.查询对应配套列表信息
        List<FacilityInfo> facilityInfoList = facilityInfoService.selectListByRoomInfoId(roomInfo.getId());
        //6.查询对应标签列表信息
        List<LabelInfo> labelInfoList = labelInfoService.selectListByRoomInfoId(roomInfo.getId());
        //7.查询对应支付方式列表信息
        List<PaymentType> paymentTypeList = paymentTypeService.selectListByRoomInfoId(roomInfo.getId());
        //8.查询对应可选租期列表信息
        List<LeaseTerm> leaseTermList = leaseTermService.selectListByRoomInfoId(roomInfo.getId());

        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除房间信息
        this.removeById(id);
        //为防止有僵尸信息存在，所以将所有的该房间相关信息进行删除
        //2.删除租期关系里面的信息
        LambdaQueryWrapper<RoomLeaseTerm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(queryWrapper);
        //3.删除支付方式里面的信息
        LambdaQueryWrapper<RoomPaymentType> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(queryWrapper1);
        //4.删除属性关系里面的信息
        LambdaQueryWrapper<RoomAttrValue> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(queryWrapper2);
        //5.删除配套关系里面的信息
        LambdaQueryWrapper<RoomFacility> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(queryWrapper3);
        //6.删除标签关系里面的信息
        LambdaQueryWrapper<RoomLabel> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(queryWrapper4);
        //7.删除图片关系里面的信息
        LambdaQueryWrapper<GraphInfo> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(queryWrapper5);
    }
}




